const { data } = require('jquery');
require('../common/header/index');
require('../common/sidebar/index');
var _util = require('../../util/util')
var _header_service = require('../../service/header-service')
var _sidebar_service = require('../../service/sidebar-service')
var _cart_service = require('../../service/cart-service');
const vue = require('../common/vue');
const _product_service = require('../../service/product-service');
let _products_content = new vue({
    _this: this,
    el: '#showItems',
    data: {
        keyword: '',
        pageid: 1,
        pagesize: '',
        sortby: 1,
        id: '',
        type: '',
        categoryid: '',
        productList: [],
        info: '',
        pagesequence: [],
        currentpage: 0
    },
    methods: {
        updateList: function (event) {
            if (!this.id) {
                _products.searchItems();
            } else {
                _products.searchProducts();
            }
        },
        backpage: function (event) {
            console.log(this.info.pages)
            if (this.pageid > 1) {
                this.pageid = this.pageid - 1;
                this.updateList();
            }
        },
        toPage: function (pagenum) {
            this.pageid = pagenum;
            this.updateList();
        },
        nextpage: function (event) {
            console.log(this.info.pages)
            console.log(this.pageid * 1 + 1 <= this.info.pages * 1)
            if (this.pageid * 1 + 1 <= this.info.pages * 1) {
                console.log("进去了")
                this.pageid = this.pageid * 1 + 1;
                this.updateList();
            }
        },
        addToCart: function(itemid) {
            _product_service.getOneItem(itemid, function(res) {
                let attrnum = res.length;
                let attrdescription = "";
                let price = res[0].price * res[0].specialrate;
                for (let i = 0; i < attrnum - 1; i++) {
                    attrdescription = attrdescription + " " + res[i].attrList[0].attrvalue;
                    price = price * res[i].attrList[0].rate;
                }
                attrdescription = attrdescription + " 单位" + res[attrnum-1].attrList[0].attrvalue;
                let content = {
                    num: 1,
                    price: price,
                    attrdescription: attrdescription
                }
                _cart_service.addToCart(itemid, function(res) {
                    window.location.href = "/view/product_summary.html";
                }, function(err) {
                    console.log(err);
                }, content);
            }, function(err) {
                console.log(err);
            });
        },
        toBlockView: function() {
            $("#listSpan").removeClass("btn-primary");
            $("#blockSpan").addClass("btn-primary");
        },
        toListView: function() {
            $("#blockSpan").removeClass("btn-primary");
            $("#listSpan").addClass("btn-primary");
        }
    },
    created: function () {
        this.keyword = decodeURI(_util.getQueryVariable('keyword'));
        this.categoryid = _util.getQueryVariable('categoryid');
        this.pageid = _util.getQueryVariable('pageid');
        this.pagesize = _util.getQueryVariable('pagesize');
        this.sortby = _util.getQueryVariable('sortby');
        this.id = _util.getQueryVariable('id');
    }
})
let _products = {
    _this: this,
    init: function () {
        this.bindEvents();
        if (!_products_content.id) {
            console.log('id为false')
            this.searchItems()
        } else {
            console.log('id为true')
            this.searchProducts()
        }
    },
    bindEvents: function () {

    },
    searchItems: function () {
        _header_service.searchItems(
            _products_content.pagesize,
            _products_content.pageid,
            _products_content.keyword,
            _products_content.categoryid,
            _products_content.sortby,
            function (res) {
                _products_content.productList = res.records;
                _products_content.pagesequence = [];
                _products_content.currentpage = res.current;
                for(let i=1;i<=res.pages;i++){
                    _products_content.pagesequence[i-1] = i;
                }
                console.log("log");
                console.log(_products_content.pagesequence);
                console.log(_products_content.currentpage);
                
                for (let i = 0; i < _products_content.productList.length; i++) {
                    var pair = _products_content.productList[i].src.split("\"");
                    _products_content.productList[i].src = "../image/" + pair[1].substring(7);
                }
                _products_content.info = res;
                console.log(_products_content.info.total)
                console.log(_products_content.productList)
            },
            function (errMsg) {
                console.log(errMsg)
            }
        )
    },
    searchProducts: function () {
        _sidebar_service.searchProducts(
            _products_content.id,
            _products_content.pagesize,
            _products_content.pageid,
            _products_content.sortby,
            function (res) {
                _products_content.productList = res.records;
                _products_content.pagesequence = [];
                _products_content.currentpage = res.current;
                for(let i=1;i<=res.pages;i++){
                    _products_content.pagesequence[i-1] = i;
                }
                console.log("log");
                console.log(_products_content.pagesequence);
                console.log(_products_content.currentpage);
                for (let i = 0; i < _products_content.productList.length; i++) {
                    var pair = _products_content.productList[i].src.split("\"");
                    _products_content.productList[i].src = "../image/" + pair[1].substring(7);
                }
                _products_content.info = res;
                console.log(_products_content.info.total)
                console.log(_products_content.productList)
            },
            function (errMsg) {
                console.log(errMsg)
            }
        )
    }
}
module.exports = _products.init();